package com.woniuxy.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.store.infra.mapper.StorePersonnelMapper;
import com.woniuxy.store.infra.model.StorePersonnel;
import com.woniuxy.store.infra.model.StoreRole;
import com.woniuxy.store.service.StoreInfoService;
import com.woniuxy.store.service.StorePersonnelService;
import com.woniuxy.store.service.StoreRoleService;
import exception.MallException;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023-07-20
 */
@Service
public class StorePersonnelServiceImpl extends ServiceImpl<StorePersonnelMapper, StorePersonnel> implements StorePersonnelService {

    @Resource
    private StoreRoleService storeRoleService;


    @Override
    public void addPersonnel(StorePersonnel personnel) {
        StorePersonnel storePerson  = getOne(new QueryWrapper<StorePersonnel>().eq("personnel_phone", personnel.getPersonnelPhone()));
        if (storePerson!=null){
            throw new MallException("人员已存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        StoreRole role = storeRoleService.getOne(new QueryWrapper<StoreRole>().eq("store_role_name", "员工"));
        personnel.setStoreRoleId(role.getStoreRoleId());
        personnel.setStoreRoleName(role.getStoreRoleName());
        save(personnel);
    }
    @Log("修改")
    @Override
    public void modifyPersonnel(StorePersonnel personnel) {
        StorePersonnel storePerson = getById(personnel.getPersonnelId());
        System.out.println(storePerson);
        if ( storePerson==null || storePerson.getStoreId().intValue()!= personnel.getStoreId().intValue()){
            throw new MallException("人员不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        StorePersonnel preson  = getOne(new QueryWrapper<StorePersonnel>().eq("personnel_phone", personnel.getPersonnelPhone()));
        if (preson!=null){
            throw new MallException("号码已被注册", CommonResultCodeEnum.INVALID_PARAM);
        }
        updateById(personnel);

    }

    @Override
    public void delPersonnel(Integer storeId, Integer personnelId) {

        StorePersonnel storePerson = getById(personnelId);
        if ( storePerson==null || storePerson.getStoreId().intValue()!= storeId){
            throw new MallException("人员不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (storePerson.getStoreRoleName().equals("老板")){
            throw new MallException("不能删除老板账号", CommonResultCodeEnum.INVALID_PARAM);
        }
        removeById(personnelId);
    }

    @Override
    public List<StorePersonnel> getPersonnel(Integer storeId) {
        QueryWrapper<StorePersonnel> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        return list(wrapper);
    }

    @Override
    public StorePersonnel login(String personnelPhone, String personnelPassword) {
        QueryWrapper<StorePersonnel> wrapper = new QueryWrapper<>();
        wrapper.eq("personnel_phone",personnelPhone);
        StorePersonnel one = getOne(wrapper);
        if (one==null){
            throw new MallException("用户不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (!one.getPersonnelPassword().equals(personnelPassword)){
            throw new MallException("密码错误", CommonResultCodeEnum.INVALID_PARAM);
        }
        return one;
    }
}
